package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * Author chongge
 * Time 2019/10/29 20:12
 * ClassName com.itheima.service.impl
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Override
    public Result submitOrder(Map map) throws Exception {
        //1.检查当前是否有预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(date);
        //预约没有设置,提示错误信息当前日期没有预约
        if (orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //预约设置了,当前预约是否已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        //满了，提示错误信息预约已满
        if (reservations >= number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //2.判断用户是否为会员:根据电话号码查询
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        // 是会员:判断当前用户是否在同一天预约同一个套餐,如果重复无法预约
        //          判断条件,会员id,日期,套餐id
        if (member != null){
            int setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(member.getId(),date,null,null,setmealId);
            order = orderDao.findByCondition(order);
            if (order != null) {
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        } else {
            // 不是会员自动注册预约
            //          姓名,性别,手机号码,身份证号码
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //3.预约成功,已预约数+1  根据预约日期查询已预约人数+1
        //套餐id,预约日期,预约类型,预约状态,会员id
        orderSettingDao.editReservations(date);
        Order order = new Order(member.getId(),
                date,(String)map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }

    /***
     * 根据Id查询预约人的信息
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Result findById(Integer id) throws Exception {
        Map map = orderDao.findById(id);
        if (map != null){
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }
        return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
    }
}
